package com.live.stamp.base.token;

import com.hikeSoft.common.utils.BaseUtils;
import com.live.stamp.base.constants.Constants;
import com.live.stamp.web.user.baseDataService.UserBaseService;
import com.live.stamp.web.user.model.TUser;
import com.live.stamp.web.user.service.UserService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.container.core.common.ServerResponse;
import org.springframework.boot.container.core.controller.BaseRestController;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;

/**
 * token 认证
 */
@Aspect
@Component
@Order(1)
public class TokenAuthAspect {

    @Autowired
    private UserBaseService userBaseService;

    @Pointcut("execution(* com.live.stamp..*Controller.*(..))")
    public void tokenAuthService() {
    }

    @Around("tokenAuthService()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        Object target = pjp.getTarget();
        if (target instanceof BaseRestController) {
            Signature signature = pjp.getSignature();
            MethodSignature methodSignature = (MethodSignature) signature;
            Method method = methodSignature.getMethod();
            if (method != null) {
                if (method.isAnnotationPresent(TokenAuth.class)) {
                    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
                    String token = request.getParameter("token");
                    if (BaseUtils.isBlank(token)) {
                        return ((BaseRestController) target).doError("token不能为空");
                    }
                    String result = TokenUtil.parseToken(token);
                    ServerResponse json = new ServerResponse();
                    if (result.equals(Constants.TOKEN_ERROR_EXP)) {
                        json.setCode(Integer.valueOf(Constants.TOKEN_ERROR_EXP));
                        json.setMsg("token过期");
                        return ((BaseRestController) target).doError("token过期");
//                        throw new BusinessException(json.getMsg(), json.getCode(), null);
                    }
                    if (result.equals(Constants.TOKEN_ERROR_SIGN)) {
                        json.setCode(Integer.valueOf(Constants.TOKEN_ERROR_EXP));
                        json.setMsg("token签名错误");
                        return ((BaseRestController) target).doError("token签名错误");
//                        throw new BusinessException(json.getMsg(), json.getCode(), null);
                    }
                    if (result.equals(Constants.TOKEN_ERROR_STR)) {
                        json.setCode(Integer.valueOf(Constants.TOKEN_ERROR_EXP));
                        json.setMsg("token非法");
                        return ((BaseRestController) target).doError("token非法");
//                        throw new BusinessException(json.getMsg(), json.getCode(), null);
                    }
                    TUser user = userBaseService.getUserById(result);

                    if(user == null) {          //如果用户信息为空
                        json.setCode(Integer.valueOf(Constants.TOKEN_ERROR_EXP));
                        json.setMsg("token过期");
                        return ((BaseRestController) target).doError("token过期");
                    }

                    new UserModelUtil(user);
                }
            } else {
                return ((BaseRestController) target).doError("该请求发生错误非法");
            }
        }
        return pjp.proceed();
    }
}
